import { Controller, Get, Post, Body, Query,Param } from '@nestjs/common';
import { ProductService } from 'src/services/product/product.service';
import { Product } from 'src/types/product.interface';
import { HttpResponse } from 'src/types/http_response.interface';
@Controller('products')
export class ProductController {
    constructor(private readonly productService: ProductService) {

    }
    //GET请求装饰器，无参数
    @Get()
    async findAllActive(): Promise<HttpResponse<Product[]>> {
        const data = await this.productService.find({ isActive: true });
        return {
            success: true,
            message: 'All active products retrieved successfully',
            data
        };
    }
    @Post()
    async create(
        @Body() createProductDTO: Product | Product[]
    ): Promise<HttpResponse<Product[] | Product>> {
        const data = await this.productService.create(createProductDTO);
        return {
            success: true,
            message: 'Product created successfully',
            data
        };
    }
    @Get('premium')//查询最新的6个精品商品
    async findPremiumProducts(): Promise<HttpResponse<Product[]>> {
        const data = await this.productService.findPremiumProducts();
        return {
            success: true,
            message: 'findPremiumProducts successfully',
            data
        };
    }
    @Get('recommended')//查询猜你喜欢的商品
    async findRecomended(
        @Query('page') page: number = 1,//要查询的页数
        @Query('limit') limit: number = 4//每页的条数
    ): Promise<HttpResponse<Product[]>> {
        const data = await this.productService.findRecomended(page, limit);
        return {
            success: true,
            message: 'findPremiumProducts successfully',
            data
        };

    }
    @Get(':id')
    async findById(@Param('id') id:string):Promise<HttpResponse<Product>>{
        const data = await this.productService.findById(id);
        return {
            success: true,
            message: 'Product retrieved successfully',
            data
        };
    }
}